/*
 * @lc app=leetcode.cn id=面试题 17.12 lang=cpp
 * @lcpr version=30204
 *
 * [面试题 17.12] BiNode
 */

// @lcpr-template-start
using namespace std;
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// @lcpr-template-end
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
// @lc code=start

class Solution
{
public:
    TreeNode *convertBiNode(TreeNode *root)
    {
        if (!root)
            return nullptr;
        vector<TreeNode *> v;
        dfs(v, root);
        v.push_back(nullptr);
        for (int i = 0; i < v.size() - 1; ++i)
        {
            v[i]->left = nullptr;
            v[i]->right = v[i + 1];
        }
        return v[0];
    }

    void dfs(vector<TreeNode *> &v, TreeNode *root)
    {
        if (root == nullptr)
        {
            return;
        }

        dfs(v, root->left);
        v.push_back(root);
        dfs(v, root->right);
    }
};
// @lc code=end

/*
// @lcpr case=start
// [4,2,5,1,3,null,6,0]\n
// @lcpr case=end

 */
